package com.bdc.whatwhat.views.cell;

import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.activities.MainActivity;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.AjouterAmisTask;
import com.bdc.whatwhat.task.DemandeWhatWhatTask;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.utils.DateUtils;
import com.bdc.whatwhat.utils.transform.CircleTransform;
import com.bdc.whatwhat.utils.transform.CropTransformation;
import com.facebook.Session;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.squareup.picasso.Picasso;

/**
 * Created by kevin on 21/06/14.
 */
public class WhatterCell extends ViewCell<Whatter> implements View.OnClickListener, AjouterAmisTask.AjouterAmisTaskTaskDelegate, DemandeWhatWhatTask.DemandeWhatWhatTaskTaskDelegate, FaireWhatWhatTask.FaireWhatWhatTaskDelegate {

    ImageView image;
    TextView prenom;
    TextView nom;
    TextView status;
    TextView nombre_personne;
    TextView date;

    View demander_en_amis;
    View demander_what_what;
    View re_what_what;
    View re_what_what_fait;
    View invitation_envoyee;

    SessionManager session;


    FaireWhatWhatTask faireWhatWhatTask;

    @Override
    public void animer() {

    }

    @Override
    public void construire() {

    }

    @Override
    public void charger() {
        this.image = (ImageView) findViewById(R.id.imageWatter);
        this.prenom = (TextView) findViewById(R.id.prenom);
        this.nom = (TextView) findViewById(R.id.nom);
        this.status = (TextView) findViewById(R.id.status);
        this.nombre_personne = (TextView) findViewById(R.id.nombre_personne);
        this.demander_en_amis = findViewById(R.id.demander_en_amis);
        this.demander_what_what = findViewById(R.id.demander_quoi_quoi);
        this.invitation_envoyee = findViewById(R.id.invitation_envoyee);
        this.re_what_what = findViewById(R.id.re_what_what);
        this.re_what_what_fait = findViewById(R.id.re_what_what_fait);
        this.date = (TextView) findViewById(R.id.cell_whatter_date);
        session = new SessionManager(getContext());
    }

    @Override
    public void remplir() {

        invitation_envoyee.setVisibility(View.GONE);
        demander_what_what.setVisibility(View.GONE);
        demander_en_amis.setVisibility(View.GONE);
        re_what_what.setVisibility(View.GONE);
        re_what_what_fait.setVisibility(View.INVISIBLE);

        if (getObject() != null) {


            if (getObject().getPrenom() != null)
                this.prenom.setText(getObject().getPrenom());

            if (getObject().getNom() != null)
                this.nom.setText(getObject().getNom());

            if (getObject().getWhatWhat() != null) {

                if (getObject().getWhatWhat().getTexte() != null)
                    this.status.setText("“" + getObject().getWhatWhat().getTexte() + "”");
                else
                    this.status.setText("Ne fais rien");

                nombre_personne.setText(getObject().getWhatWhat().getNombre());

                try {
                    if (session.getProfil().getWhatWhat().getId().equals(getObject().getWhatWhat().getId()))
                        re_what_what_fait.setVisibility(View.VISIBLE);
                }catch (Exception e){
                    e.printStackTrace();
                }

                if(getObject().getWhatWhat().getDateFaire() != null){
                    date.setVisibility(View.VISIBLE);
                    date.setText(DateUtils.formaterDate(getObject().getWhatWhat().getDateFaire(),getContext()));
                }
            }

            if (getObject().getUrlImage() != null && !getObject().getUrlImage().trim().equalsIgnoreCase("")) {
                Picasso.with(context).load(getObject().getUrlImage()).transform(new CropTransformation(300)).transform(new CircleTransform()).into(image);
            } else {
                Log.e("Picasso", "error");
            }


            if (getObject().isAmi()) {
                demander_what_what.setVisibility(View.VISIBLE);
                demander_what_what.setOnClickListener(this);
                re_what_what.setVisibility(View.VISIBLE);
                re_what_what.setOnClickListener(this);
            } else {

                if (getObject().isDemande()) {
                    invitation_envoyee.setVisibility(View.VISIBLE);
                } else {
                    demander_en_amis.setVisibility(View.VISIBLE);
                    demander_en_amis.setOnClickListener(this);
                }
            }



        }

    }

    @Override
    public void ajouterListeners() {
        getView().setOnClickListener(this);
        findViewById(R.id.cell_content).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.demander_en_amis:
                new AjouterAmisTask(this).execute(session.getToken(), getObject().getId());
                break;

            case R.id.re_what_what:
                faire(getObject().getWhatWhat());
                break;

            case R.id.demander_quoi_quoi:
                new DemandeWhatWhatTask(this).execute(session.getToken(), getObject().getId());
                animerDemandeQuoiQuoi();
                break;
        }

    }

    private void animerDemandeQuoiQuoi() {
        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.playTogether(
                new ObjectAnimator().ofFloat(demander_what_what, "scaleX", 1, 0.9f,1),
                new ObjectAnimator().ofFloat(demander_what_what, "scaleY", 1, 0.9f,1)
        );
        animatorSet.start();
    }

    @Override
    public void faire(WhatWhat whatWhat) {
        if (faireWhatWhatTask != null)
            faireWhatWhatTask.cancel(true);
        faireWhatWhatTask = new FaireWhatWhatTask(this, FaireWhatWhatTask.EXISTANT);
        faireWhatWhatTask.execute(whatWhat, session.getToken());

        animerFaire();
    }

    @Override
    public void afficherChargement(boolean afficher) {
    }

    public void animerFaire(){
        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.playTogether(
                new ObjectAnimator().ofFloat(re_what_what_fait, "scaleX", 0, 1),
                new ObjectAnimator().ofFloat(re_what_what_fait, "scaleY", 0, 1)
        );
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                re_what_what_fait.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        getView().postDelayed(new Runnable() {
            @Override
            public void run() {
                animatorSet.start();
            }
        },300);

    }

    @Override
    public void onWhatWhatEnvoye(WhatWhat whatWhat) {
        try{
            Whatter profil = session.getProfil();
            profil.setWhatWhat(whatWhat);
            session.setProfil(profil);
            ((MainActivity)getContext()).notifyProfilChanged(profil,null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onResultatVide() {

    }

    @Override
    public void onDemandeWhatWhatEnvoye() {
        Toast.makeText(getContext(), getContext().getResources().getString(R.string.demande_whatwhat_envoyee), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAmisAjouter() {
        findViewById(R.id.invitation_envoyee).setVisibility(View.VISIBLE);
        demander_en_amis.setVisibility(View.GONE);
    }

    @Override
    public void onErreurReseau() {
        Toast.makeText(getContext(), getContext().getResources().getString(R.string.erreur_reseau), Toast.LENGTH_SHORT).show();
    }
}
